package com.example.demo.ImgTest;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.image.v2.region.ImageRegion;
import com.huaweicloud.sdk.image.v2.*;
import com.huaweicloud.sdk.image.v2.model.*;

/**
 *  图像标签
 *
 *  自然图像的语义内容非常丰富，一个图像包含多个标签内容，图像标签服务准确识别自然图片中数百种场景、上千种通用物体及其属性，
 *  让智能相册管理、照片检索和分类、基于场景内容或者物体的广告推荐等功能更加直观。使用时用户发送待处理图片，返回图像标签内容及相应置信度。
 */
public class HuaWeiImgB {

    public static void main(String[] args) {
        // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全
        // 本示例以ak和sk保存在环境变量中来实现身份验证为例，运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK
        String ak = "D4V7FPPVAS2W3YRJSPYF";
        String sk = "dKmJyVJtxe4zn3mfvVVgTpSqtQB36MADe8H4reSz";

        ICredential auth = new BasicCredentials().withAk(ak).withSk(sk);

        //此处替换为您开通服务的区域
        ImageClient client = ImageClient.newBuilder()
                .withCredential(auth)
                .withRegion(ImageRegion.valueOf("cn-north-4"))
                .build();

        RunImageTaggingRequest request = new RunImageTaggingRequest();
        ImageTaggingReq body = new ImageTaggingReq();
        body.withThreshold(10f);
        body.withLanguage("zh");
        body.withLimit(1);
        //此处替换为公网可以访问的图片地址
        body.withUrl("https://img.zcool.cn/community/01346958ff6ef0a801214550ad655e.jpg@1280w_1l_2o_100sh.jpg");
//        body.withUrl("http://114.115.166.192:9910/api/1.0/assets/BUSINESS/system/2024/1705029691866e38b44b7bb30660c6a37fddd.jpg?preview=true");
//        body.withUrl("https://wnd.agri114.cn/file/cropguard/img/classifyPest/蚂蚱.png");
        request.withBody(body);
        try {
            RunImageTaggingResponse response = client.runImageTagging(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

